<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="分类ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag
                type="list"
                v-model="model.categoryId"
                dictCode="ht_library_category,name,id"
                placeholder="请选择分类ID"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="店铺ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
              <j-search-select-tag v-model="model.shopId" dict="ht_shop,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archiveName">
              <a-input v-model="model.archiveName" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="SEO关键字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seoKeywords">
              <a-textarea v-model="model.seoKeywords" rows="4" placeholder="请输入SEO关键字" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="SEO描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seoDescription">
              <a-textarea v-model="model.seoDescription" rows="4" placeholder="SEO描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="预览文件（.pdf）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="previewArchiveUrl"
            >
              <j-upload v-model="model.previewArchiveUrl"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="资料Url（.pdf）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archiveUrl">
              <j-upload v-model="model.archiveUrl"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="资料下载权限"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="archiveDownloadPermission"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.archiveDownloadPermission"
                dictCode="library_archive_download_permission"
                placeholder="请选择资料下载权限"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="副标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subname">
              <a-textarea v-model="model.subname" rows="4" placeholder="请输入副标题" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="摘要" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archiveAbstract">
              <a-textarea v-model="model.archiveAbstract" rows="4" placeholder="请输入摘要" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详细内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <j-editor v-model="model.content" />
            </a-form-model-item>
          </a-col>
          <video-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" v-model="model.videoUrls" />
          <a-col :span="24">
            <a-form-model-item label="主图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cover">
              <j-image-upload isMultiple v-model="model.cover"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片集" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="images">
              <j-image-upload isMultiple v-model="model.images"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="成本价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costPrice">
              <a-input-number v-model="model.costPrice" placeholder="请输入成本价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="产品标价（原价）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="marketPrice"
            >
              <a-input-number v-model="model.marketPrice" placeholder="请输入产品标价（原价）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="零售价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="retailPrice">
              <a-input-number v-model="model.retailPrice" placeholder="请输入零售价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="是否为首页推荐 0-不推荐 1-推荐"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="isHomeRecommended"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.isHomeRecommended"
                dictCode="is_home_recommended"
                placeholder="请选择是否为首页推荐 0-不推荐 1-推荐"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="资料状态（0-仓库中 1-下架，2 上架）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="libraryStatus"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.libraryStatus"
                dictCode="library_status"
                placeholder="请选择资料状态（0-仓库中 1-下架，2 上架）"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditStatus">
              <j-dict-select-tag
                type="list"
                v-model="model.auditStatus"
                dictCode="audit_status"
                placeholder="请选择审批状态"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditUsername">
              <a-input v-model="model.auditUsername" placeholder="请输入审批人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditComments">
              <a-textarea v-model="model.auditComments" rows="4" placeholder="请输入审批意见" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'HTLibraryForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        archiveDownloadPermission: 0,
        isHomeRecommended: 0,
        libraryStatus: '0',
        auditStatus: '0'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        categoryId: [{ required: true, message: '请输入分类ID!' }],
        shopId: [{ required: true, message: '请输入店铺ID!' }],
        archiveName: [{ required: true, message: '请输入标题!' }],
        archiveDownloadPermission: [{ required: true, message: '请输入资料下载权限!' }],
        // sort: [{ required: true, message: '请输入排序!' }],
        // homeSort: [{ required: true, message: '请输入首页排序!' }],
        // content: [{ required: true, message: '请输入详细内容!' }],
        // costPrice: [{ required: true, message: '请输入成本价!' }],
        // marketPrice: [{ required: true, message: '请输入产品标价（原价）!' }],
        // retailPrice: [{ required: true, message: '请输入零售价!' }],
        isHomeRecommended: [{ required: true, message: '请输入是否为首页推荐 0-不推荐 1-推荐!' }],
        libraryStatus: [{ required: true, message: '请输入资料状态（0-仓库中 1-下架，2 上架）!' }],
        auditStatus: [{ required: true, message: '请输入审批状态!' }]
      },
      url: {
        add: '/library/hTLibrary/add',
        edit: '/library/hTLibrary/edit',
        queryById: '/library/hTLibrary/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>

